package com.zmn.oms.zmn.business.impl.log;

import com.zmn.oms.model.entity.log.OrderChannelDockingLog;
import com.zmn.oms.third.daowei.DaoweiConsts;
import com.zmn.oms.third.tc58.Tc58Consts;
import com.zmn.oms.zmn.business.interfaces.log.OrderChannelDockingLogBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述：
 *
 * @author lhl
 * @date 2020/07/07 11:36
 */
@Service("orderChannelDockingLogBService")
public class OrderChannelDockingLogBServiceImpl implements OrderChannelDockingLogBService {

    /**
     * 支持跟踪记录的渠道
     */
    private Map<String, Boolean> channelFilter = new HashMap<>();
    {
        Map<String, Boolean> filter = channelFilter;
        // 到位系列
        filter.put(String.valueOf(DaoweiConsts.CHANNEL_ID_APP), Boolean.TRUE);
        filter.put(String.valueOf(DaoweiConsts.CHANNEL_ID_DTW), Boolean.TRUE);
        filter.put(String.valueOf(DaoweiConsts.CHANNEL_ID_KYX), Boolean.TRUE);
        filter.put(String.valueOf(DaoweiConsts.CHANNEL_ID_ZX), Boolean.TRUE);
        // 58同城系列
        filter.put(String.valueOf(Tc58Consts.CHANNEL_ID_VIP), Boolean.TRUE);
        filter.put(String.valueOf(Tc58Consts.CHANNEL_ID_FWK), Boolean.TRUE);
        filter.put(String.valueOf(Tc58Consts.CHANNEL_ID_JX), Boolean.TRUE);
        filter.put(String.valueOf(Tc58Consts.CHANNEL_ID_JX_PIPE), Boolean.TRUE);
        filter.put(String.valueOf(Tc58Consts.CHANNEL_ID_JX_CLOCK), Boolean.TRUE);
    }

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void save(OrderChannelDockingLog orderChannelDockingLog) {
        mongoTemplate.save(orderChannelDockingLog);
    }

    @Override
    public List<OrderChannelDockingLog> listByOrderId(Long orderId) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("orderId").is(orderId);
        query.addCriteria(criteria);
        query.with(Sort.by(Sort.Order.desc("beginTime")));
        return mongoTemplate.find(query, OrderChannelDockingLog.class);
    }

    @Override
    public List<OrderChannelDockingLog> listByOrderIdAndChannelId(Long orderId, Integer channelId) {
        if (channelId == null || channelFilter.get(String.valueOf(channelId.intValue())) == null) {
            return null;
        }

        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("orderId").is(orderId);
        query.addCriteria(criteria);
        query.with(Sort.by(Sort.Order.desc("beginTime")));
        return mongoTemplate.find(query, OrderChannelDockingLog.class);
    }
}
